package net.adetech.dcweb.core.utils.configuration;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by sunfeihu on 2017/4/18.
 */
@Configuration
public class DruidConfiguration {

    public static Logger LOG = LoggerFactory.getLogger(DruidConfiguration.class);

    @Value("${spring.datasource.driverClassName}")
    String driverClass;
    @Value("${spring.datasource.url}")
    String url;
    @Value("${spring.datasource.username}")
    String username;
    @Value("${spring.datasource.password}")
    String password;

    @Value("${spring.datasource_oracle.driverClassName}")
    String oracleDriverClass;
    @Value("${spring.datasource_oracle.url}")
    String oracleUrl;
    @Value("${spring.datasource_oracle.username}")
    String oracleUsername;
    @Value("${spring.datasource_oracle.password}")
    String oraclePassword;


    @Bean(name = "oracleDataSource")
    @ConfigurationProperties(prefix="spring.datasource_oracle")
    public DataSource oracleDataSource() {
        return DataSourceBuilder.create().build();
    }


    @Bean(name = "oracleJdbcTemplate")
    public JdbcTemplate oracleJdbcTemplate(
            @Qualifier("oracleDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "mysqlDataSource")
    @Primary
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource mysqlDataSource() {
        return DataSourceBuilder.create().build();
    }



    @Bean(name = "jdbcTemplate")
    @Primary
    public JdbcTemplate mysqlJdbcTemplate(
            @Qualifier("mysqlDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }


//    @Bean
//    public ServletRegistrationBean druidServlet() {
//        return new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
//    }
//
//    @Bean
//    public DataSource druidDataSource() {
//        DruidDataSource ds = new DruidDataSource();
//        ds.setDriverClassName(driverClass);
//        ds.setUrl(url);
//        ds.setUsername(username);
//        ds.setPassword(password);
//        List<String> sqls = new ArrayList<String>();
//        sqls.add("set names utf8mb4;");
//        ds.setConnectionInitSqls(sqls);
//        try {
//            ds.setFilters("stat,wall");
//        } catch (SQLException e) {
//            e.printStackTrace();
//            LOG.error("druid err:{}", e);
//        }
//        return ds;
//    }
//
//    @Bean
//    public FilterRegistrationBean filterRegistrationBean() {
//        FilterRegistrationBean fb = new FilterRegistrationBean();
//        fb.setFilter(new WebStatFilter());
//        fb.addUrlPatterns("/*");
//        fb.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
//        return fb;
//    }
//
//    @Bean
//    public DataSourceTransactionManager dataSourceTransactionManager() {
//        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
//        dataSourceTransactionManager.setDataSource(druidDataSource());
//        return dataSourceTransactionManager;
//    }

}
